package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @PreAuthorize("hasAnyAuthority('ROLE_ADD')")
    @RequestMapping("add")
    public Result add(@RequestParam("permissionIds") Integer[] permissionIds,
                      @RequestParam("menuIds")Integer[] menuIds,
                      @RequestBody Role role) {
        try {
            roleService.add(role, permissionIds,menuIds);
            return new Result(true,"新增角色数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增角色数据失败");
        }
    }

    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        try {
            PageResult pageResult = roleService.findPage(queryPageBean);
            return new Result(true,"查询角色数据成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色数据失败");
        }

    }

    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    @RequestMapping("findById")
    public Result findById(@RequestParam("id")Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, "查询角色数据成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色数据失败");
        }
    }

    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    @RequestMapping("findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(@RequestParam("id")Integer id) {
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true,"获取权限数据ID成功", permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取权限数据ID失败");
        }
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EDIT')")
    @RequestMapping("edit")
    public Result edit(@RequestParam("permissionIds") Integer[] permissionIds,
                       @RequestParam("menuIds")Integer[] menuIds,
                       @RequestBody Role role) {
        try {
            roleService.edit(role, permissionIds,menuIds);
            return new Result(true,"编辑角色数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑角色数据失败");
        }
    }

    @PreAuthorize("hasAnyAuthority('ROLE_DELETE')")
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id")Integer id) {
        try {
            roleService.deleteById(id);
            return new Result(true,"删除角色数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色数据失败");
        }
    }

    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    @RequestMapping("findAll")
    public Result findAll() {
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true,"查询角色数据成功",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色数据失败");
        }
    }
}
